library(qualtRics)
library(dplyr)
library(estimatr)
library(tidyr)
library(ggplot2)
library(tidyverse)
library(magrittr)

study3 <- qualtRics::read_survey("data/study3.csv") %>%
 filter(quality > 0 | is.na(quality)) %>%
 filter(is.na(generic_flag)) %>%
 filter(!grepl("farm", generic_flag)) %>%
 filter(as.Date(StartDate, format='%m/%d/%Y')>="2022-12-12")

study3[which(study3$hhi == '-3105'), "hhi"] <- NA
study3[which(study3$age == '-3105'), "age"] <- NA
study3[which(study3$education == '-3105'), "education"] <- NA
study3[which(study3$ethnicity == '-3105'), "ethnicity"] <- NA
study3[which(study3$hispanic == '-3105'), "hispanic"] <- NA
study3[which(study3$political_party == '-3105'), "political_party"] <- NA

study3 %<>%
 mutate(polsoph1 = as.numeric(polsoph1 == 2),
        polsoph2 = as.numeric(polsoph2 == 1),
  polsoph3 = as.numeric(grepl("2/3|thirds|67", polsoph3, ignore.case = T)),
        polsoph4 = as.numeric(grepl("court|judicial|scotus", polsoph4, ignore.case = T)),
        polsoph5 = as.numeric(grepl("vice|vp", polsoph5, ignore.case = T)))

study3$pk_scale <- study3 %>% select(contains("polsoph")) %>% rowSums()/5

study3 %>%
 select(contains("post_certain_m")) %>%
 psych::alpha() -> post_certain

study3$post_certain_scores <- post_certain$scores
study3$white <- as.numeric(study3$ethnicity == 1)
study3$correct_likert <- as.numeric(grepl("^\n\nI believe|^I believe", study3$likert))

theme_params <-  theme(panel.background = element_rect(fill = 'gray95'),
                       panel.grid.minor = element_blank(),
                       panel.grid.major = element_blank(),
                       legend.text = element_text(size = 15),
                       axis.text.x = element_text(size = 15),
                       legend.title=element_blank(),
                       axis.text.y = element_text(size = 15),
                       axis.title.y = element_text(size = 15),
                       axis.title.x = element_text(size = 15),
                       legend.position = "bottom",
                       plot.margin = unit(c(.25,.25,.25,.25), "cm"),
                       strip.text.x = element_text(size = 15))
